package org.dtrd.modules.shop.service.impl;

import org.dtrd.modules.shop.entity.po.DtrdEntShcRdOrderJoinProduct;
import org.dtrd.modules.shop.mapper.DtrdEntShcRdOrderJoinProductMapper;
import org.dtrd.modules.shop.service.IDtrdEntShcRdOrderJoinProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.dynamic.datasource.annotation.DS;
import org.dtrd.modules.shop.entity.bean.OrderJoinProductDetail;
import org.dtrd.modules.shop.entity.bean.OrderJoinProductListInfo;
import org.dtrd.modules.shop.entity.bean.OrderJoinProductListRequest;
import org.dtrd.modules.shop.entity.bean.OrderJoinProductPageRequest;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;


/**
 * <p>
 * 金蝶商品与订单的关联关系表 服务实现类
 * </p>
 *
 * @author sun
 * @since 2022-07-23
 */
@DS("multi-datasource1")
@Service
public class DtrdEntShcRdOrderJoinProductServiceImpl extends ServiceImpl<DtrdEntShcRdOrderJoinProductMapper, DtrdEntShcRdOrderJoinProduct> implements IDtrdEntShcRdOrderJoinProductService {

    private DtrdEntShcRdOrderJoinProductMapper dtrdEntShcRdOrderJoinProductMapper;

    @Override
    public List<OrderJoinProductListInfo> getOrderJoinProductList(OrderJoinProductListRequest request) {
        LambdaQueryWrapper<DtrdEntShcRdOrderJoinProduct> queryWrapper = baseQueryWrapper();
        List<DtrdEntShcRdOrderJoinProduct> poList = list(queryWrapper);
        List<OrderJoinProductListInfo> dtoList = new ArrayList<>(poList.size());
        if (!poList.isEmpty()) {
            dtoList = poList.stream().map(OrderJoinProductListInfo::new).collect(Collectors.toList());
        }
        return dtoList;
    }

    @Override
    public IPage<OrderJoinProductListInfo> pageOrderJoinProduct(OrderJoinProductPageRequest request) {
        LambdaQueryWrapper<DtrdEntShcRdOrderJoinProduct> queryWrapper = baseQueryWrapper();
        IPage<DtrdEntShcRdOrderJoinProduct> page = new Page<>(request.getPageNum(), request.getPageSize());
        page = this.page(page, queryWrapper);
        return page.convert(OrderJoinProductListInfo::new);
    }

    @Override
    public OrderJoinProductDetail getOrderJoinProduct(Integer dataId) {
        DtrdEntShcRdOrderJoinProduct dtrdEntShcRdOrderJoinProduct = getById(dataId);
        return Optional.ofNullable(dtrdEntShcRdOrderJoinProduct)
                        .map(po -> new OrderJoinProductDetail().parseFromPo(po))
                        .orElse(null);
    }

    @Override
    public boolean saveOrUpdateOrderJoinProduct(OrderJoinProductDetail dto) {
        DtrdEntShcRdOrderJoinProduct dtrdEntShcRdOrderJoinProduct = new DtrdEntShcRdOrderJoinProduct();
        if(dto.getDataId() == null){
            dtrdEntShcRdOrderJoinProduct = dtrdEntShcRdOrderJoinProduct.init();
        }
        dtrdEntShcRdOrderJoinProduct.parseFromDto(dto);
        return saveOrUpdate(dtrdEntShcRdOrderJoinProduct);
    }

    @Override
    public boolean removeOrderJoinProduct(Integer dataId) {
        return removeById(dataId);
    }

    private LambdaQueryWrapper<DtrdEntShcRdOrderJoinProduct> baseQueryWrapper() {
        return Wrappers.lambdaQuery(DtrdEntShcRdOrderJoinProduct.class)
                        .eq(DtrdEntShcRdOrderJoinProduct::getIsDel, 0);
    }

    private LambdaUpdateWrapper<DtrdEntShcRdOrderJoinProduct> baseUpdateWrapper() {
        return Wrappers.lambdaUpdate(DtrdEntShcRdOrderJoinProduct.class)
                        .eq(DtrdEntShcRdOrderJoinProduct::getIsDel, 0);
    }
}
